package com.mostromas.ess.product.controller;

import com.mostromas.ess.entities.ProductCategory;
import com.mostromas.ess.product.req.ProductCategoryREQ;
import com.mostromas.ess.product.service.IProductCategoryService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 商品分类表-前端控制器
 */
@Api(value = "商品分类管理接口",description = "分类管理接口，提供分类的增、删、改、查")
@RestController //所有方法都会返回json字符串进行相应
@RequestMapping("/category")
public class ProductCategoryController {

    @Autowired
    private IProductCategoryService categoryService;

    /**
     * 分页条件查询分类信息
     * @param req
     * @return
     */
    @PreAuthorize(("hasAuthority('category:search')"))
    @ApiOperation("根据分类名称、状态或父类ID查询分类列表接口")
    @PostMapping("/search") //请求路径： /product/category/search
    public Result search(@RequestBody ProductCategoryREQ req){
        return categoryService.queryPage(req);
    }

    @PreAuthorize(("hasAuthority('category:search')"))
    @ApiOperation("查询类别详情接口")
    @ApiImplicitParam(name = "id",value = "类别ID",required = true)
    @GetMapping("/{id}") //请求路径：/product/category/{id}
    public Result view(@PathVariable("id") String id){
        ProductCategory productCategory = categoryService.getById(id);
        return Result.ok(productCategory);
    }

    @PreAuthorize(("hasAuthority('category:edit')"))
    @ApiOperation("修改类别信息接口")
    @PutMapping //put方式调用接口：/product/category
    public Result update(@RequestBody ProductCategory productCategory){
        categoryService.updateById(productCategory);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('category:add')"))
    @ApiOperation("新增类别信息接口")
    @PostMapping //post方式调用接口：/product/category
    public Result save(@RequestBody ProductCategory productCategory){
        categoryService.save(productCategory);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('category:delete')"))
    @ApiOperation("删除类别信息接口")
    @ApiImplicitParam(name = "id",value = "类别ID",required = true)
    @DeleteMapping("/{id}") //delete方式调用接口：/product/category/{id}
    public Result delete(@PathVariable("id") String id){
        categoryService.removeById(id);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('category:search')"))
    @ApiOperation("获取正常状态的类别信息接口")
    @GetMapping("/list") //请求路径：/product/category/list
    public Result list(){
        return categoryService.findAllNormal();
    }
}
